package idv.arthur.work;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DelMRitem
 */
public class SendMail extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SendMail() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String fromAddr = request.getParameter("fromaddr");
		String fromAddrName = request.getParameter("fromaddrname");
		String toAddr = request.getParameter("toaddr");
		String toAddrName = request.getParameter("toaddrname");
		String subject = request.getParameter("subject");
		String msgBody = request.getParameter("msgbody");

		String clientIP = request.getRemoteAddr();

		msgBody = msgBody + "\n IP: " + clientIP; // 執行者IP位址

		sendMailMain(fromAddr, fromAddrName, toAddr, toAddrName, subject,msgBody);

	}
	private  void sendMailMain( String fromAddr, String fromAddrName, String toAddr, String toAddrName, String subject, String msgBody) { 


        Properties prop = new Properties();
        prop.put("mail.smtp.host", "10.160.1.1");  //設定SMTP Server

        Session session = Session.getDefaultInstance(prop, null); //與SMTP Server取得連線

        try{

                MimeMessage msg = new MimeMessage(session);

                
                //主旨
                msg.setSubject(new String( subject.getBytes("UTF-8"), "UTF-8" ),"UTF-8");

                //寄件人
                //InternetAddress from = new InternetAddress("MBMS@cht.com.tw");
                InternetAddress from = new InternetAddress(fromAddr);
                from.setPersonal(fromAddrName, "UTF-8");  //設定寄件人姓名
                msg.setFrom(from);


                //收件人
//               Address address = new InternetAddress(toAddr);
//               msg.setRecipient(Message.RecipientType.TO, address);
                 InternetAddress to = new InternetAddress(toAddr);
                 to.setPersonal(toAddrName, "UTF-8");
                 msg.setRecipient(Message.RecipientType.TO, to);


                //Address address = new InternetAddress("dolaamn@cht.com.tw");             //收件人
                //msg.setRecipient(Message.RecipientType.TO, address);

                //InternetAddress ccAddress = new InternetAddress("dolaamn@cht.com.tw");   //副本人
                //msg.addRecipient(Message.RecipientType.CC, ccAddress);


                Multipart mp = new MimeMultipart();

                //設定本文區的內容
                MimeBodyPart mbp1 = new MimeBodyPart();
                mbp1.setContent(msgBody, "text/plain;charset=UTF-8");
                mp.addBodyPart(mbp1);


                msg.setContent(mp);

                Transport.send(msg);

        } catch (MessagingException me) {
             me.printStackTrace();
        } catch (Exception e) {
             e.toString();
        }
}
}